package de.bazinga.rotw.menu.item;

import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Text;
import com.jme.scene.shape.Quad;

public class TextItem extends BasicMenuItem {
	private static final long serialVersionUID = 1L;
	
	
	private Text textObj = null;
	private String text = "";
	private int startLength = 0;
	
	private String command = "";
	private ColorRGBA activeColor = null;
	private ColorRGBA inactiveColor = null;
	
	private Quad bgQuad = null;
	
	public TextItem(String text, String command, ColorRGBA activeColor, ColorRGBA inactiveColor, float scale,float x, float y) {
		super(command);
		// Text
		this.text = text;
		this.startLength = text.length();
		this.activeColor = activeColor;
		this.inactiveColor = inactiveColor;
		this.command = command;
		
		textObj = Text.createDefaultTextLabel(command, text);
		textObj.setTextureCombineMode(TextureCombineMode.Replace);
		
		textObj.setTextColor(inactiveColor);
		
		attachChild(textObj);
		
		setLocalTranslation(x, y, 0);
		setLocalScale(scale);
		
		
		updateGeometricState(0.0f, true);
		updateRenderState();
		

		KeyBindingManager.getKeyBindingManager().add("shift",KeyInput.KEY_LSHIFT);
		
		KeyBindingManager.getKeyBindingManager().set("A",
	               KeyInput.KEY_A);
	       KeyBindingManager.getKeyBindingManager().set("B",
	               KeyInput.KEY_B);
	       KeyBindingManager.getKeyBindingManager().set("C",
	               KeyInput.KEY_C);
	       KeyBindingManager.getKeyBindingManager().set("D",
	               KeyInput.KEY_D);
	       KeyBindingManager.getKeyBindingManager().set("E",
	               KeyInput.KEY_E);
	       KeyBindingManager.getKeyBindingManager().set("F",
	               KeyInput.KEY_F);
	       KeyBindingManager.getKeyBindingManager().set("G",
	               KeyInput.KEY_G);
	       KeyBindingManager.getKeyBindingManager().set("H",
	               KeyInput.KEY_H);
	       KeyBindingManager.getKeyBindingManager().set("I",
	               KeyInput.KEY_I);
	       KeyBindingManager.getKeyBindingManager().set("J",
	               KeyInput.KEY_J);
	       KeyBindingManager.getKeyBindingManager().set("K",
	               KeyInput.KEY_K);
	       KeyBindingManager.getKeyBindingManager().set("L",
	               KeyInput.KEY_L);
	       KeyBindingManager.getKeyBindingManager().set("M",
	               KeyInput.KEY_M);
	       KeyBindingManager.getKeyBindingManager().set("N",
	               KeyInput.KEY_N);
	       KeyBindingManager.getKeyBindingManager().set("O",
	               KeyInput.KEY_O);
	       KeyBindingManager.getKeyBindingManager().set("P",
	               KeyInput.KEY_P);
	       KeyBindingManager.getKeyBindingManager().set("Q",
	               KeyInput.KEY_Q);
	       KeyBindingManager.getKeyBindingManager().set("R",
	               KeyInput.KEY_R);
	       KeyBindingManager.getKeyBindingManager().set("S",
	               KeyInput.KEY_S);
	       KeyBindingManager.getKeyBindingManager().set("T",
	               KeyInput.KEY_T);
	       KeyBindingManager.getKeyBindingManager().set("U",
	               KeyInput.KEY_U);
	       KeyBindingManager.getKeyBindingManager().set("V",
	               KeyInput.KEY_V);
	       KeyBindingManager.getKeyBindingManager().set("W",
	               KeyInput.KEY_W);
	       KeyBindingManager.getKeyBindingManager().set("X",
	               KeyInput.KEY_X);
	       KeyBindingManager.getKeyBindingManager().set("Y",
	               KeyInput.KEY_Y);
	       KeyBindingManager.getKeyBindingManager().set("Z",
	               KeyInput.KEY_Z);
	       
		KeyBindingManager.getKeyBindingManager().set("1",
	               KeyInput.KEY_1);
		KeyBindingManager.getKeyBindingManager().set("2",
               KeyInput.KEY_2);
		KeyBindingManager.getKeyBindingManager().set("3",
               KeyInput.KEY_3);
		KeyBindingManager.getKeyBindingManager().set("4",
               KeyInput.KEY_4);
		KeyBindingManager.getKeyBindingManager().set("5",
               KeyInput.KEY_5);
		KeyBindingManager.getKeyBindingManager().set("6",
               KeyInput.KEY_6);
		KeyBindingManager.getKeyBindingManager().set("7",
               KeyInput.KEY_7);
		KeyBindingManager.getKeyBindingManager().set("8",
               KeyInput.KEY_8);
		KeyBindingManager.getKeyBindingManager().set("9",
               KeyInput.KEY_9);
		KeyBindingManager.getKeyBindingManager().set("0",
               KeyInput.KEY_0);
		

	       
		KeyBindingManager.getKeyBindingManager().set(".",
				KeyInput.KEY_PERIOD);
		KeyBindingManager.getKeyBindingManager().set(":",
				KeyInput.KEY_COLON);
		
		KeyBindingManager.getKeyBindingManager().set("del",
				KeyInput.KEY_BACK);
	}
	
	public void setTextColor(ColorRGBA color) {
		textObj.setTextColor(color);
		textObj.updateRenderState();
		
		updateRenderState();
		
	}
	
	
	
	public void setText(String text) {
		this.text = text;
		this.detachAllChildren();
		
		textObj = Text.createDefaultTextLabel(command, text);
		textObj.setTextureCombineMode(TextureCombineMode.Replace);
		
		if(isActive()) textObj.setTextColor(activeColor);
		else textObj.setTextColor(inactiveColor);
		
		attachChild(textObj);
		
		updateGeometricState(0.0f, true);
		updateRenderState();
	}
	
	
	public void update() {
		if(isActive()) {
			String[] letters = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
			String appendLetter = "";
			
			
			for(String l:letters) {
				if(KeyBindingManager.getKeyBindingManager().isValidCommand(l, false)) {
					appendLetter = l;
				}
			}
			
			if(KeyBindingManager.getKeyBindingManager().isValidCommand("1", false)) {
				appendLetter = "1";
			}
			if(KeyBindingManager.getKeyBindingManager().isValidCommand("2", false)) {
				appendLetter = "2";
			}
			if(KeyBindingManager.getKeyBindingManager().isValidCommand("3", false)) {
				appendLetter = "3";
			}
			if(KeyBindingManager.getKeyBindingManager().isValidCommand("4", false)) {
				appendLetter = "4";
			}
			if(KeyBindingManager.getKeyBindingManager().isValidCommand("5", false)) {
				appendLetter = "5";
			}
			if(KeyBindingManager.getKeyBindingManager().isValidCommand("6", false)) {
				appendLetter = "6";
			}
			if(KeyBindingManager.getKeyBindingManager().isValidCommand("7", false)) {
				appendLetter = "7";
			}
			if(KeyBindingManager.getKeyBindingManager().isValidCommand("8", false)) {
				appendLetter = "8";
			}
			if(KeyBindingManager.getKeyBindingManager().isValidCommand("9", false)) {
				appendLetter = "9";
			}
			if(KeyBindingManager.getKeyBindingManager().isValidCommand("0", false)) {
				appendLetter = "0";
			}
			
			if(KeyBindingManager.getKeyBindingManager().isValidCommand(".", false)) {
				appendLetter = ".";
			}
			
			if(KeyBindingManager.getKeyBindingManager().isValidCommand(":", false)) {
				appendLetter = ":";
			}

			if(KeyBindingManager.getKeyBindingManager().isValidCommand("shift")) {
				appendLetter = appendLetter.toUpperCase();
			}else{
				appendLetter = appendLetter.toLowerCase();
			}
			this.setText(getText()+appendLetter);

			if(KeyBindingManager.getKeyBindingManager().isValidCommand("del", false)) {
				int currLength = getText().length();
				if(currLength > startLength) {
					this.setText(getText().substring(0, currLength-1));
				}
			}
			
			
		}
	}
	
	
	
	public float getX() {
		return getLocalTranslation().x;
	}
	
	public float getY() {
		return getLocalTranslation().y;
	}
	
	public float getWidth() {
		if(textObj != null) {
			return textObj.getWidth();
		}else if(bgQuad != null) {
			return bgQuad.getWidth();
		}
		return 0;
	}
	
	public float getHeight() {
		if(textObj != null) {
			return textObj.getHeight();
		}else if(bgQuad != null) {
			return bgQuad.getHeight();
		}
		return 0;
	}
	
	public void setActive(boolean active) {
		super.setActive(active);
		
		if(active) {
			setTextColor(activeColor);
		}else{
			setTextColor(inactiveColor);
		}
	}
	
	public String getText() {
		return text;
	}
	
	public String getTextOutput() {
		return text.substring(this.startLength);
	}
}

